package com.gerfmarquez.nymph.db.sqlite;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.gerfmarquez.nymph.db.sqlite.dto.Song;
import com.gerfmarquez.nymph.db.sqlite.dto.SongNoteLength;

public class SongNoteLengthDbAdapter {
	public static final String KEY_ID_SONG = "id_song";
	public static final String KEY_ID_NOTE = "id_music_note";
	public static final String KEY_ID_LENGTH = "id_note_length";
	private static final String SONG_NOTE_LENGTH_TABLE = "song_note_length";
	
	private SQLiteDatabase database;
	
	public SongNoteLengthDbAdapter(SQLiteDatabase database) {
		this.database = database;
	}
	
	public Song getSong() {
		return new Song();
	}
	
	

	public void createSongNoteLength(long idSong, int note, int length) {
			ContentValues initialValues = createContentValues(idSong, note, length);
			database.insert(SONG_NOTE_LENGTH_TABLE, null, initialValues);
	}

	
/** * Update the todo */

	public boolean updateTodo(long rowId, String category, String summary,
			String description) {
//		ContentValues updateValues = createContentValues(category, summary,
//				description);
//
//		return database.update(SONG_TABLE, updateValues, KEY_ROWID + "="
//				+ rowId, null) > 0;
		return false;
	}

	
/** * Deletes todo */

	public boolean deleteSongNoteLength(long idSong) {
		return database.delete(SONG_NOTE_LENGTH_TABLE, KEY_ID_SONG + "=" + idSong, null) > 0;
	}

	
/** * Return a Cursor over the list of all todo in the database * * @return Cursor over all notes */

	public Cursor fetchAllTodos() {
//		return database.query(SONG_TABLE, new String[] { KEY_ROWID,
//				KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION }, null, null, null,
//				null, null);
		return null;
	}

	
/** * Return a Cursor positioned at the defined todo */

	public ArrayList<SongNoteLength> fetchSongNoteLength(long idSong) throws SQLException {
		ArrayList<SongNoteLength> songNoteLength = new ArrayList<SongNoteLength>();
		Cursor mCursor = database.query(SONG_NOTE_LENGTH_TABLE, new String[] {
				KEY_ID_SONG, KEY_ID_NOTE, KEY_ID_LENGTH},
				KEY_ID_SONG + "=" + idSong, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
			while(!mCursor.isAfterLast()) {
				SongNoteLength songNoteLengthTemp = new SongNoteLength();
 
				int note = mCursor.getInt(mCursor.getColumnIndex(KEY_ID_NOTE));
				int idLength = mCursor.getInt(mCursor.getColumnIndex(KEY_ID_LENGTH));
				
				NoteLengthDbAdapter noteLengthAdapter = new NoteLengthDbAdapter(database);
				int length = noteLengthAdapter.getNoteLengthFromId(idLength);
				
				songNoteLengthTemp.setMusicNote(note);
				songNoteLengthTemp.setNoteLength(length);
				
				songNoteLength.add(songNoteLengthTemp);
				
				mCursor.moveToNext();
			}
		}
		
		return songNoteLength ;
	}

	private ContentValues createContentValues(long id_song ,int note, int rhythm) {
		ContentValues values = new ContentValues();
		values.put(KEY_ID_SONG, id_song);
		values.put(KEY_ID_NOTE, note);
		values.put(KEY_ID_LENGTH, rhythm);
		return values;
	}
}
